<template>
  <div class="yb-card-html">
    <el-cascader title="医保电子凭证，请选择正常参保地，再刷码" style="width:150px" v-model="insuplc_admdvs" placeholder="异地医保凭证请选择正确参保地" :props="props"
                 popper-class="el_cascader_class" filterable></el-cascader>

    <el-input type="text" v-model.trim="KNSJ" clearable style="width:370px;" placeholder="请将鼠标光标定位在此处再扫二维码"
              @keyup.enter.native="enterFunction()" @clear="clearBack()">
      <div slot="append">
        <el-button class="mr10" @click="SEND_ReadCardBasYB" :loading="yb_loading">医保卡</el-button>
        <el-button @click="ReadSFZYB" :loading="sfz_loading">身份证</el-button>

      </div>
    </el-input>
    <el-link type="primary" @click="open_sm" style="font-size:12px;margin-left: 5px;">使用说明</el-link>
  </div>
</template>

<script>
import websocket from '@/modules/CST/utils/webSocket';
import { GET_ADDRESS_LIST } from "_SERVICES/api";

export default {
  name: 'YbCard',
  props: {

  },
  computed: {

  },
  data() {
    return {
      KNSJ: "",
      obj: {},
      yb_loading: false,
      sfz_loading: false,
      insuplc_admdvs: ["44000000", "44030000"],
      props: {
        value: "DM",
        label: "MC",
        children: "children",
        lazy: true,
        emitPath: true,
        lazyLoad: this.lazyLoad
      }
    };
  },
  created() {
    if (!this.$store.state.WEBSOCKET) { //医保卡4 、身份证通道3
      // if(process.env.VUE_APP_build_type == 'development' || process.env.VUE_APP_build_type == 'test') { //如果本地测试可以打开 默认关闭
      //   return false;
      // }
      websocket.init(this.$eventHub);

      setTimeout(() => {
        console.log("医保初始化");
        this.Init_yb()
      }, 400)
    }
    this.$eventHub.$on('ReadCard_Back', (data) => {
      this.yb_loading = false;
      this.sfz_loading = false;
      if (data.code != 0) {
        this.KNSJ = ""
      } else {
        this.KNSJ = "********"
      }
      this.$emit("back", data)
    });
  },
  methods: {

    enterFunction() {
      if (!this.KNSJ) {
        return false;
      }
      let insuplc_admdvs = ""
      if(this.insuplc_admdvs && this.insuplc_admdvs.length==2){
        insuplc_admdvs = this.insuplc_admdvs[1];
      }

      this.$emit("back", {
        code: 0,
        WYBS: this.KNSJ,
        insuplc_admdvs: insuplc_admdvs,
        BSLX: 1  //自费卡刷卡、电子健康码、医保电子凭证都走这里 又后端去自行做判断。
      })

      setTimeout(() => {
        this.KNSJ = "********";
      }, 200)

      //   this.$emit("back",this.obj)
    },
    clear() {
      this.KNSJ = "";
    },
    clearBack() {
      this.$emit("back", {
        code: -1,
      })
    },
    //医保卡
    SEND_ReadCardBasYB() {
      this.yb_loading = true;
      this.$store.state.WEBSOCKET.send('{"plcc":{"cmdType":"ReadCardBasYB","plccType":"SSCARD-MHAH-31"},"param":{}}');
    },
    //身份证
    ReadSFZYB() {
      this.sfz_loading = true;
      this.$store.state.WEBSOCKET.send('{"plcc":{"cmdType":"ReadSFZYB","plccType":"SSCARD-MHAH-31"},"param":{}}');
    },
    //医保读扫码信息
    GetQRBaseYB() {
      this.$store.state.WEBSOCKET.send('{"plcc":{"cmdType":"ReadSFZYB","plccType":"SSCARD-MHAH-31"},"param":{}}');
    },

    Init_yb() {
      var pUrl = "http://hosp-sz.gd.hsip.gov.cn/gdyb_api/prd/api/card/initDll";
      var pUser = "440300";
      var param = { param1: pUrl, param2: pUser };
      var plcc = { plcc: { cmdType: "Init", plccType: "SSCARD-MHAH-31" }, param: param };//请求服务数据包
      //发送到websocket服务器
      var json = JSON.stringify(plcc);
      this.$store.state.WEBSOCKET.send(json);
    },
    open_sm() {
      this.$alert(`
      1、自费卡、电子健康码、电子医保凭证直接定位光标在输入框，刷卡刷码即可（刷卡刷码之前确保输入框无内容）。
      <br>2、医保卡、身份证，需要将卡插在扫码墩指定的位置，然后点击【医保卡/身份证】按钮
      <br>3、对于使用<span class='color-danger'>电子医保凭证的患者，请先选择正确的参保地，再刷码！！！</span>
      `, '三合一扫码墩使用说明', {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: true,
      });
    },
    lazyLoad(node, resolve) {
      const { level, value } = node;
      console.log(level, value)
      let ADDRESS = value || "";
      //因为设置了初始的默认值，这里会自动去调用
      GET_ADDRESS_LIST({
        ADDRESS: ADDRESS,
        TYPE: 0
      }).then((res) => {
        let list = res.data || [];
        list.forEach((item) => {
          if (level == 1) {
            item.leaf = 'leaf';
            delete item.children;
          }
        })
        resolve(list)
      })

      // if(level==0){

      // }
      // if (level == 1) {
      //   GET_ADDRESS_LIST({
      //     ADDRESS: ADDRESS,
      //     TYPE: 0
      //   }).then((res) => {
      //     let list = res.data || [];
      //     list.forEach((item) => {
      //       item.leaf = 'leaf';
      //       delete item.children;
      //     })
      //     resolve(list)
      //   })
      // }
    }

  }
}
</script>

<style  lang="less">
.el_cascader_class {
  .el-cascader-menu__wrap {
    height: 300px;
  }
}
</style>
